<template>
  <app-list>
    <template #search>
      <div class="setting-title">授权管理</div>
    </template>
    <template #action>
      <el-button type="primary" class="hovepcvicecolor" icon="plus" @click="handleadd()">新增授权</el-button>
    </template>
    <el-table stripe :data="pageData.list" v-loading="pageData.loading">
      <el-table-column label="ID" prop="id" show-overflow-tooltip />
      <el-table-column label="三方平台" prop="pg_name" show-overflow-tooltip />
      <el-table-column label="组织" prop="sub_name" show-overflow-tooltip />
      <el-table-column label="授权范围" prop="authority" show-overflow-tooltip>
        <template #default="scope">
          <div>{{ authorityLabel(scope.row.authority) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" show-overflow-tooltip>
        <template #default="scope">
          <div>{{ scope.row.created_at }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <div style="display: flex">
            <div class="csspointer" @click="handleEdit(scope.row)">编辑</div>
            <div class="csspointer" style="margin-left: 12px" @click="deleteloginrule(scope.row)">删除</div>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #footerRight>
      <el-pagination
        @size-change="pageActions.handleSizeChange"
        @current-change="pageActions.handlePageChange"
        v-model:current-page="pageData.page"
        v-model:page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
        background
      />
    </template>

    <AddItem
      v-model="dialog.edit.show"
      :data="dialog.edit.data"
      :parentdata="parentdata"
      @change="changeform()"
    />
  </app-list>
</template>
  
  <script setup>
import { http } from '@/utils/http'
import { ElMessage } from 'element-plus'

import AddItem from '../AddItem.vue'

const typeMap = ref({
  1: '自动登录登出',
  2: '保持登录',
})

const parentdata = ref({
  value: 'authorize',
  label: '新增授权',
})

const { pageData, pageActions, dialog } = usePage(
  () => {
    const query = {
      page: pageData.page,
      page_size: pageData.pageSize,
      ...pageData.query,
    }

    return http.get('/accredit/plant_group_list', {
      params: query,
    })
  },
  {
    query: {},
  }
)

const timetrans = inject('timetrans')

// 新增
const handleadd = () => {
  parentdata.value.status = 'create'
  parentdata.value.label = '新增授权'
  pageActions.handleAdd()
}

// 编辑
const handleEdit = row => {
  parentdata.value.status = 'update'
  parentdata.value.label = '编辑授权'
  pageActions.handleEdit(row)
}

const changeform = () => {
  pageActions.refresh(true)
}

const deleteloginrule = row => {
  const onSuccess = () => {
    ElMessage.success('删除成功')
    changeform()
  }
  http({
    method: 'DELETE',
    url: `/accredit/delete_plant_group?id=${row.id}`,
  }).then(onSuccess)
}

const authorityLabel = value => {
  let a = {
    memeber: '会员',
    ip: 'IP',
    order: '订单'
  }
  let b = ''
  value.forEach((item, index)=> {
    if (index == 0) {
      b += a[item]
    } else {
      b += `、${a[item]}`
    }
  });
  return b
}
</script>
  
  <style lang="scss" scoped>
</style>